﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Nuwanda.Common;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Nuwanda.TfsSpotlight.Shell.Data;

namespace Nuwanda.TfsSpotlight.Data
{
    /// <summary>Describes server</summary>
    [Author("Bartłomiej Legiędź")]
    [Serializable]
    public class ServerDescriptor : BaseEntity<ServerDescriptor>
    {
        private string _name;
        /// <summary>Server display name</summary>
        [StringLengthValidator(1, RangeBoundaryType.Inclusive, 0, RangeBoundaryType.Ignore, MessageTemplate = "Podaj nazwę serwera")]
        public string Name
        {
            [DebuggerStepThrough]
            get { return this._name; }
            set
            {
                this._name = value;
                OnPropertyChanged("Name");
            }
        }
        
        private string _url;
        /// <summary>Server URL</summary>
        public string Url
        {
            [DebuggerStepThrough]
            get { return this._url; }
            set
            { 
                this._url = value;
                base.OnPropertyChanged("Url");
            }
        }

        private bool _connectAtStartup;
        /// <summary>States whether the application should connect to this server at startup</summary>
        public bool ConnectAtStartup
        {
            [DebuggerStepThrough]
            get { return this._connectAtStartup; }
            set
            {
                this._connectAtStartup = value;
                base.OnPropertyChanged("ConnectAtStartup");
            }
        }

        private List<ProjectDescriptor> _projects;
        /// <summary>Projects retrieved from the server</summary>
        [XmlIgnore]
        public List<ProjectDescriptor> Projects
        {
            [DebuggerStepThrough]
            get { return _projects; }
        }

        public ServerDescriptor()
        {
            this.ConnectAtStartup = true;
            this._projects = new List<ProjectDescriptor>();
        }

        public ServerDescriptor(string name, string url)
        {
            this.Name = name;
            this.Url = url;
            this._projects = new List<ProjectDescriptor>();
            this.ConnectAtStartup = true;
        }

        public override bool Equals(object obj)
        {
            if(obj is ServerDescriptor)
            {
                var server = obj as ServerDescriptor;
                return server.Url.Equals(this.Url);
            }

            return false;
        }

        public override int GetHashCode()
        {
            return this.Url.GetHashCode();
        }

        public string GetUrlForFileName()
        {
            var url = this.Url;
            var indexOfPrefix = this.Url.IndexOf("://");
            
            if(indexOfPrefix >= 0)
            {
                url = url.Remove(0, indexOfPrefix + 3);
            }

            url = url.Replace('/', '.').Replace(':', '.');

            return url;
        }
    }
}
